package cn.rongdao.jrkabao.ui.remind.fragment;

import android.view.View;
import android.widget.RelativeLayout;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frtools.util.FRToast;

import java.util.List;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseFragment;
import cn.rongdao.jrkabao.bean.remind.RemindBillBean;
import cn.rongdao.jrkabao.model.remind.RemindCreditCardBillsModel;
import cn.rongdao.jrkabao.treeview.TreeNode;
import cn.rongdao.jrkabao.treeview.TreeView;
import cn.rongdao.jrkabao.treeview.TreeViewAdapter;
import cn.rongdao.jrkabao.ui.cashadvance.uicomponent.CashEmptyUIComponent;
import cn.rongdao.jrkabao.ui.remind.RemindDetailsActivity;
import cn.rongdao.jrkabao.ui.remind.treeviewbinder.MyNodeViewFactory;

/**
 * Created by jack on 2017/11/21
 */

public class FragmentBill extends BaseFragment {

    @BindView(R.id.fb_rl_main)
    RelativeLayout llContainer;
    @BindView(R.id.fb_empty_view)
    CashEmptyUIComponent cashEmptyUIComponent;

    private TreeNode root;
    private TreeView treeView;
    private int tempLevel = 0;//节点的level
    private String remindId;
    private TreeNode child;//子节点
    private int flag = 0;
    private boolean isMoXieExport;

    @Override
    protected int layoutRes() {
        return R.layout.fragment_bill;
    }

    @Override
    protected void onViewReallyCreated(View view) {
        super.onViewReallyCreated(view);
        initData();
        initListener();
    }

    private void initListener() {
        cashEmptyUIComponent.setBtnOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((RemindDetailsActivity) getActivity()).startMoXieWithEmail();
            }
        });
    }

    private void getBillsDataDetails(String date) {
        RemindCreditCardBillsModel.getInstance().creditCardBillsDetail(remindId, date, new FRBaseObserver<List<RemindBillBean>>(this) {
            @Override
            protected void onBaseNext(List<RemindBillBean> data) {
                buildTree(data);
            }
        });
    }

    public void initData() {
        if (FRString.isEmpty(remindId)) {
            root = TreeNode.root();
            buildExampleTree();
            treeView = new TreeView(root, getContext(), new MyNodeViewFactory());
            llContainer.addView(treeView.getView());
            return;
        }
        RemindCreditCardBillsModel.getInstance().creditCardBills(remindId, new FRBaseObserver<List<RemindBillBean>>(this) {
            @Override
            protected void onBaseNext(List<RemindBillBean> data) {
                if (null != data && data.size() > 0) {
                    llContainer.setVisibility(View.VISIBLE);
                    cashEmptyUIComponent.setVisibility(View.GONE);
                    flag = 0;
                    buildTree(data);
                } else {
                    llContainer.setVisibility(View.GONE);
                    cashEmptyUIComponent.setRemindBillEmpty();
                }
            }

            @Override
            protected void onBaseError(Throwable t) {
                super.onBaseError(t);
                cashEmptyUIComponent.setRemindBillEmpty();
            }
        });
    }

    private void buildTree(List<RemindBillBean> list) {
        if (null == list || list.size() == 0) {
            if (null != child) {
                FRToast.showToastSafe("暂无账单详情");
            }
            return;
        }
        TreeNode parent;
        if (null == child) {   //第一层
            root = TreeNode.root();
            for (RemindBillBean remindBillBean : list) {
                parent = new TreeNode(remindBillBean);
                parent.setLevel(tempLevel);
                root.addChild(parent);
            }
            //生成树形布局图
            treeView = new TreeView(root, getContext(), new MyNodeViewFactory());
            llContainer.addView(treeView.getView());
            flag += 1;
        } else {   //第N层
            for (RemindBillBean remindBillBean : list) {
                parent = new TreeNode(remindBillBean);
                parent.setLevel(tempLevel);
                if (child.isSelected()) {//先勾选，在请求孩子的情况
                    parent.setSelected(true);
                }
                //添加孩子
                child.addChild(parent);
            }
            //请求完成展开节点
            treeView.expandNode(child);
        }
        if (flag == 1) {
            treeView.setAdapterFirstLevelClickListener(new TreeViewAdapter.OnItemClickListener() {
                @Override
                public void click(TreeNode treeNode) {
                    if (isMoXieExport) {
                        FRToast.showToastSafe("账单正在导入中，请稍候再试");
                        return;
                    }
                    treeNode.setExpanded(!treeNode.isExpanded());
                    if (treeNode.isExpanded()) {
                        //打开节点
                        if (treeNode.getChildren().size() == 0) {
                            //第一次打开节点.需要请求次数据
                            String month = ((RemindBillBean) treeNode.getValue()).getMonth();
                            child = treeNode;
                            tempLevel = treeNode.getLevel() + 1;
                            getBillsDataDetails(month);
                        } else {
                            //第N次打开节点
                            treeView.expandNode(treeNode);
                        }
                    } else {
                        //关闭节点
                        treeView.collapseNode(treeNode);
                    }
                }
            });
        }
    }

    private void buildExampleTree() {
        for (int i = 0; i < 3; i++) {
            RemindBillBean remindBillBean = new RemindBillBean();
            remindBillBean.setAmount("10000.00");
            if (i == 0) {
                remindBillBean.setMonth("12/2017");
                remindBillBean.setDate("11.04-12.04");
            } else if (i == 1) {
                remindBillBean.setMonth("11/2017");
                remindBillBean.setDate("10.04-11.03");
            } else {
                remindBillBean.setMonth("10/2017");
                remindBillBean.setDate("09.04-10.03");
            }
            TreeNode treeNode = new TreeNode(remindBillBean);
            treeNode.setLevel(MyNodeViewFactory.FIRST_LEVEL);
            for (int j = 0; j < 3; j++) {
                RemindBillBean remindBillBean1 = new RemindBillBean();
                if (j == 0) {
                    remindBillBean1.setBills_amount("7000");
                    remindBillBean1.setBills_date("11.04");
                    remindBillBean1.setBills_desc("买电脑");
                } else if (j == 1) {
                    remindBillBean1.setBills_amount("2000");
                    remindBillBean1.setBills_date("11.04");
                    remindBillBean1.setBills_desc("买手机");
                } else {
                    remindBillBean1.setBills_amount("150000.00");
                    remindBillBean1.setBills_date("11.04");
                    remindBillBean1.setBills_desc("买汽车");
                }
                TreeNode treeNode1 = new TreeNode(remindBillBean1);
                treeNode1.setLevel(MyNodeViewFactory.SECOND_LEVEL);
                treeNode.addChild(treeNode1);
            }
            root.addChild(treeNode);
        }
    }

    public void setIsMoXieExport(boolean isMoXieExport) {
        this.isMoXieExport = isMoXieExport;
    }

    public void setRemindId(String remindId) {
        this.remindId = remindId;
    }
}